﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameLibrary.Map
{
    using Tile;
    /// <summary>
    /// Store game map.
    /// Readonly. Singleton.
    /// Use after set width and height in GMHelper.
    /// </summary>
    [Serializable]
    public sealed class TileMap
    {
        #region variables

        private static TileType[,] map;

        #endregion

        #region constructor

        private TileMap()
        {
            map = GMHelper.GenerateMap();
        }

        #endregion

        #region this
        /// <summary>
        /// If position fall out of map range return null,
        /// else return tiletype of that position.
        /// </summary>
        /// <param name="k1"></param>
        /// <param name="k2"></param>
        /// <returns></returns>
        public TileType this[int k1,int k2]
        {
            get
            {
                if (k1 > GMHelper.Width || k2 > GMHelper.Height)
                    return null;
                else
                    return map[k1, k2];
            }
        }

        #endregion

        #region singleton

        private class TileMapGetter
        {
            static TileMapGetter() { }
            internal static readonly TileMap UniqueInstance = new TileMap();
        }

        public static TileMap UniqueInstance
        {
            get
            {
                return TileMapGetter.UniqueInstance;
            }
        }

        #endregion

        #region change tile type

        public bool ChangeTileType(Position pos, TileType tile)
        {
            if (pos.X < GMHelper.Width && pos.Y < GMHelper.Height)
            {
                map[pos.X, pos.Y] = tile;
                return true;
            }
            else
                return false;
        }

        #endregion
    }
}
